package me.jessyan.armscomponent.app.mvp.ui.activity.learn;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.mvp.model.entity.SignItem;
import me.jessyan.armscomponent.app.mvp.model.entity.SignResponse;
import me.jessyan.armscomponent.app.mvp.ui.activity.di.component.DaggerSignActivityComponent;
import me.jessyan.armscomponent.app.mvp.contract.learn.SignActivityContract;
import me.jessyan.armscomponent.app.mvp.presenter.learn.SignActivityPresenter;

import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.commonsdk.utils.Utils.WeekDay;


import static com.jess.arms.utils.Preconditions.checkNotNull;
import static me.jessyan.armscomponent.commonsdk.utils.Utils.getWeekDay;


/**
 * ================================================
 * Description: 打卡页面
 * <p>
 * Created by MVPArmsTemplate on 10/21/2021 10:43
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@Route(path = RouterHub.LEARNING_SIGNACTIVITY)
public class SignActivity extends BaseActivity<SignActivityPresenter> implements SignActivityContract.View {

    @BindView(R.id.sign_week)
    LinearLayout sign_week;

    @BindView(R.id.sign_result)
    LinearLayout sign_result;

    @BindView(R.id.sign_recommand)
    LinearLayout sign_recommand;


    @OnClick({R.id.user_btn_sign})
    public void onClicked(View v) {
        int id = v.getId();
        if (id == R.id.user_btn_sign) {
            if (Utils.isFastClick()) {
                toSign();
            }
        }
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSignActivityComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_sign; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.loadSignList();//获取用户打卡记录
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private void toSign(){
        mPresenter.sign();
    }

    @Override
    public void rendSignList(SignResponse result) {
        sign_week.removeAllViews();

        List<WeekDay> list = getWeekDay();
        List<SignItem> items = result.getRows();
        //填充打卡记录
        for (WeekDay weekday : list) {
            // weekday.day   01
            // weekday.dayLong  2021-11-01
            // weekday.week  周一
            //System.out.println(weekday.toString());

            boolean isSign = false;
            for (int i = 0; i < items.size(); i++) {
                SignItem signItem = items.get(i);
                String itemTime = Utils.getDateToString(signItem.getCreateTime());

                if (itemTime.equals(weekday.dayLong)) {
                    isSign = true;
                    break;
                }
            }

            RelativeLayout recentLyItem = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_sign_item, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            lp.setMargins(8, 5, 8, 0);
            recentLyItem.setLayoutParams(lp);

            TextView sign_item_bg = recentLyItem.findViewById(R.id.sign_item_bg);
            TextView sign_item_day = recentLyItem.findViewById(R.id.sign_item_day);
            TextView sign_item_week = recentLyItem.findViewById(R.id.sign_item_week);

            sign_item_day.setText(weekday.day + "");
            sign_item_week.setText(weekday.week + "");

            if (isSign){//已签到
                sign_item_bg.setBackgroundResource(R.drawable.round_sign_little_selector);

                sign_item_day.setTextColor(Color.parseColor("#fff"));
                sign_item_day.setBackgroundResource(R.drawable.round_sign_center_selector);

                sign_item_week.setTextColor(Color.parseColor("#fff"));
            }

            sign_week.addView(recentLyItem);
        }

    }
}
